<template>
  <div 
    ref="containerRef"
    class="container-widget" 
    :class="{ 
      'is-selected': isSelected,
      'nested': getContainerLevel() > 0
    }"
    :style="containerStyle"
  >
    <!-- 子组件内容 -->
    <slot />
    
    <!-- 空容器提示 -->
    <div v-if="!hasChildren" class="empty-container">
      <div class="empty-tip">
        <i class="el-icon-plus"></i>
        <span>拖拽组件到这里</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, inject, ref, onMounted, onUnmounted } from 'vue'

const props = defineProps({
  direction: {
    type: String,
    default: 'vertical'
  },
  justify: {
    type: String,
    default: 'start'
  },
  align: {
    type: String,
    default: 'start'
  },
  children: {
    type: Array,
    default: () => []
  },
  isSelected: {
    type: Boolean,
    default: false
  }
})

// 注入画布相关信息
const designerStore = inject('designerStore', null)
const currentComponent = inject('currentComponent', null)

// 响应式状态
const containerRef = ref(null)
const canvasWidth = ref(1200) // 默认画布宽度

// 计算是否有子组件
const hasChildren = computed(() => {
  // 优先使用 props.children
  if (props.children && props.children.length > 0) {
    return true
  }
  
  // 备用方案：通过 DOM 查询检测子组件
  if (containerRef.value) {
    const childComponents = containerRef.value.querySelectorAll('.component-renderer')
    return childComponents.length > 0
  }
  
  return false
})

// 获取画布宽度
const getCanvasWidth = () => {
  if (containerRef.value) {
    const canvasContent = containerRef.value.closest('.canvas-content')
    if (canvasContent) {
      // 获取画布内容区域的宽度，减去内边距
      const computedStyle = window.getComputedStyle(canvasContent)
      const paddingLeft = parseFloat(computedStyle.paddingLeft)
      const paddingRight = parseFloat(computedStyle.paddingRight)
      canvasWidth.value = canvasContent.clientWidth - paddingLeft - paddingRight
    }
  }
}

// 检测容器层级
const getContainerLevel = () => {
  if (!containerRef.value) return 0
  
  let level = 0
  let currentElement = containerRef.value.parentElement
  
  // 向上查找，计算嵌套层级
  while (currentElement && !currentElement.classList.contains('canvas-content')) {
    if (currentElement.classList.contains('component-renderer') || 
        currentElement.classList.contains('container-widget')) {
      level++
    }
    currentElement = currentElement.parentElement
  }
  
  return level
}

// 计算容器宽度
const getContainerWidth = () => {
  const level = getContainerLevel()
  
  if (level === 0) {
    // 根级容器：占满画布宽度
    return `${canvasWidth.value}px`
  } else {
    // 嵌套容器：使用相对宽度，减去内边距
    const padding = level * 8 // 每层增加8px内边距
    return `calc(100% - ${padding * 2}px)`
  }
}

const containerStyle = computed(() => {
  const flexDirection = props.direction === 'horizontal' ? 'row' : 'column'
  const justifyContent = props.justify === 'start' ? 'flex-start' : 
                       props.justify === 'center' ? 'center' :
                       props.justify === 'end' ? 'flex-end' :
                       props.justify === 'space-between' ? 'space-between' : 'flex-start'
  
  const alignItems = props.align === 'start' ? 'flex-start' :
                    props.align === 'center' ? 'center' :
                    props.align === 'end' ? 'flex-end' :
                    props.align === 'stretch' ? 'stretch' : 'flex-start'
  
  const level = getContainerLevel()
  const padding = level > 0 ? level * 8 : 0
  
  return {
    display: 'flex',
    flexDirection,
    justifyContent,
    alignItems,
    width: getContainerWidth(),
    minHeight: '120px',
    position: 'relative',
    padding: `${padding}px`
  }
})

// 生命周期
onMounted(() => {
  // 获取画布宽度
  getCanvasWidth()
  
  // 监听窗口大小变化，重新计算画布宽度
  window.addEventListener('resize', getCanvasWidth)
  
  // 使用 ResizeObserver 监听画布大小变化
  if (containerRef.value) {
    const canvasContent = containerRef.value.closest('.canvas-content')
    if (canvasContent && window.ResizeObserver) {
      const resizeObserver = new ResizeObserver(() => {
        getCanvasWidth()
      })
      resizeObserver.observe(canvasContent)
    }
  }
})

// 清理函数
onUnmounted(() => {
  window.removeEventListener('resize', getCanvasWidth)
})
</script>

<style scoped>
.container-widget {
  position: relative;
  border: 1px dashed #e4e7ed;
  background-color: #ffffff;
  padding: 0;
  transition: all 0.3s ease;
  min-height: 120px;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  width: 100%;
}

/* 嵌套容器样式 */
.container-widget.nested {
  border-color: #d9ecff;
  background-color: #f0f9ff;
  box-shadow: 0 1px 3px rgba(64, 158, 255, 0.1);
}

.container-widget.nested:hover {
  border-color: #409eff;
  background-color: #ecf5ff;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.15);
}

.container-widget:hover {
  border-color: #c0c4cc;
  background-color: #fafbfc;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

/* 选中状态样式 - 隐藏默认边框 */
.container-widget.is-selected {
  border-color: transparent;
}

.empty-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 120px;
  padding: 16px;
}

.empty-tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 8px;
  color: #909399;
}

.empty-tip i {
  font-size: 20px;
  color: #c0c4cc;
}

.empty-tip span {
  font-size: 12px;
  color: #909399;
}
</style> 